package com.sgb.kjwl.view.ui.activity.self.shop;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.View;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCompanyImgBinding;
import com.sgb.kjwl.utils.GlideEngine;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.ShopEvent;
import com.sgb.kjwl.view.event.self.ShopImageEvent;
import com.sgb.kjwl.view.ui.adapter.self.publish.ImageAdapter;
import com.sgb.kjwl.viewmodel.shopping.ShopConmpanyInfoModel;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

public class SelectCompanyImageActivity extends AppActivity {
    private ActivityCompanyImgBinding binding;

    private ImageAdapter adapter;
    //--------------企业图片----------------
    private List<LocalMedia> companyLocal;
    private List<File> companyFile;
    private List<String> companyUrls;
    //--------------证书-------------------
    private List<LocalMedia> certificateLocal;
    private List<File> certificateFile;
    private List<String> certificateUrls;
    //--------------企业视频----------------
    private List<LocalMedia> videoLocal;
    private List<File> videoFile;
    //判断显示哪一个数据
    private int type;
    private String title;
    private SensorManager sensorManager;
    private SensorEventListener sensorEventListener;

    @Inject
    ShopConmpanyInfoModel viewModel;

    public static void start(Context context, String title, int type) {
        Intent intent = new Intent(context, SelectCompanyImageActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("title", title);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorEventListener = new JCVideoPlayer.JCAutoFullscreenListener();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initView() {
        title = getIntent().getStringExtra("title");
        type = getIntent().getIntExtra("type", 10);
        binding = DataBindingUtil.setContentView(SelectCompanyImageActivity.this, R.layout.activity_company_img);
        binding.titleNameTv.setText(title);
        binding.backView.setOnClickListener(this);
        binding.addRl.setOnClickListener(this);
        binding.titleDeleteTv.setOnClickListener(this);
        if (type == 0) {
            binding.imgLl.setVisibility(View.VISIBLE);
            adapter = new ImageAdapter(this, null, null, 5);
        } else if (type == 1) {
            binding.imgLl.setVisibility(View.VISIBLE);
            adapter = new ImageAdapter(this, null, null, 10);
        } else if (type == 2) {
            binding.videoLl.setVisibility(View.VISIBLE);
        }
        binding.recyclerView.setLayoutManager(new GridLayoutManager(SelectCompanyImageActivity.this, 4));
        binding.recyclerView.setAdapter(adapter);

    }

    @Override
    protected void init() {
        //----------企业图片图片用到的list
        companyUrls = new ArrayList<>();
        companyFile = new ArrayList<>();
        //----------相关证书用到的list
        certificateUrls = new ArrayList<>();
        certificateFile = new ArrayList<>();

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                backSetImage();
                finish();
                break;
            case R.id.add_rl:
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofVideo())
                        .loadImageEngine(GlideEngine.createGlideEngine())
                        .theme(R.style.picture_default_style)
                        .maxSelectNum(1)//判断还能选多少张图片
                        .selectionMedia(videoLocal)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.title_delete_tv:
                videoLocal = null;
                binding.videoView.setVisibility(View.GONE);
                binding.addRl.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (JCVideoPlayer.backPress()) {
            return;
        }
        backSetImage();
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    if (type == 0) {
                        companyLocal = selectList;
                        companyFile.clear();
                        companyUrls.clear();
                        for (LocalMedia localMedia : selectList) {
                            File file = new File(localMedia.getRealPath());
                            companyFile.add(file);
                            companyUrls.add(localMedia.getRealPath());
                        }
                        adapter.setDatas(selectList, companyUrls);
                        adapter.setNetImgList(companyUrls);
                        adapter.setFileList(companyFile);
                    } else if (type == 1) {
                        certificateLocal = selectList;
                        certificateFile.clear();
                        certificateUrls.clear();
                        for (LocalMedia localMedia : selectList) {
                            File file = new File(localMedia.getRealPath());
                            certificateFile.add(file);
                            certificateUrls.add(localMedia.getRealPath());
                        }
                        adapter.setDatas(selectList, certificateUrls);
                        adapter.setFileList(certificateFile);
                    } else if (type == 2) {
                        videoLocal = selectList;
                        binding.addRl.setVisibility(View.GONE);
                        binding.videoView.setVisibility(View.VISIBLE);
                        binding.titleDeleteTv.setVisibility(View.VISIBLE);
                        binding.videoView.setUp(videoLocal.get(0).getRealPath(), JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "企业视频");
                    }
                    break;
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(sensorEventListener);
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(ShopImageEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_SHOP_COMPANY_RE_IMG) {
            //企业图片
            companyUrls = (List<String>) event.getMessageData();
            companyLocal = (List<LocalMedia>) event.getObject();
            if (companyLocal != null)
                for (LocalMedia localMedia : companyLocal) {
                    File file = new File(localMedia.getRealPath());
                    companyFile.add(file);
                }
            adapter.setDatas(companyLocal, companyUrls);
            adapter.setFileList(companyFile);
        } else if (event.getMessageTag() == EventConstant.EVENT_SHOP_CERTIFICATE_RE_IMG) {
            //相关证书
            certificateUrls = (List<String>) event.getMessageData();
            certificateLocal = (List<LocalMedia>) event.getObject();
            for (LocalMedia localMedia : certificateLocal) {
                File file = new File(localMedia.getRealPath());
                certificateFile.add(file);
            }
            adapter.setDatas(certificateLocal, certificateUrls);
            adapter.setFileList(certificateFile);
        } else if (event.getMessageTag() == EventConstant.EVENT_SHOP_COMPANY_RE_VIDEO) {
            videoLocal = (List<LocalMedia>) event.getMessageData();
            binding.addRl.setVisibility(View.GONE);
            binding.videoView.setVisibility(View.VISIBLE);
            binding.videoView.setUp(videoLocal.get(0).getRealPath(), JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "企业视频");
        }
        EventBus.getDefault().removeStickyEvent(event);
    }


    //返回时把选择的数据发送到上一个界面
    private void backSetImage() {
        if (type == 0) {
            companyUrls = adapter.getUrls();
            companyLocal = adapter.getLocalMedia();
            EventBus.getDefault().post(ShopEvent.make().setMessageTag(EventConstant.EVENT_SHOP_COMPANY_IMG).setMessageData(companyUrls).setObject(companyLocal));
        } else if (type == 1) {
            certificateUrls = adapter.getUrls();
            certificateLocal = adapter.getLocalMedia();
            EventBus.getDefault().post(ShopEvent.make().setMessageTag(EventConstant.EVENT_SHOP_CERTIFICATE_IMG).setMessageData(certificateUrls).setObject(certificateLocal));
        } else if (type == 2) {
            EventBus.getDefault().post(ShopEvent.make().setMessageTag(EventConstant.EVENT_SHOP_COMPANY_VIDEO).setMessageData(videoLocal));
        }
    }
}
